import {
  createRouter,
  createWebHistory,
  createWebHashHistory
} from "vue-router";
import Layout from "@/layout/index.vue";

export const constantRoutes = [{
    path: "/",
    name: "home",
    redirect: "/task"
  },
  {
    path: "/task",
    name: "task",
    redirect: "/task/start",
    component: Layout,
    meta: {
      title: "我的事务",
      icon: "menu-user",
      roles: ["admin"]
    },
    children: [{
        path: "start",
        name: "TaskStart",
        component: () => import("@/views/my-flow/flow-start.vue"),
        meta: {
          title: "发起流程",
          roles: ["admin"]
        },
      },
      /* {
        path: "start/apply",
        name: "TaskStartApply",
        component: () => import("@/views/my-flow/flow-start-form.vue"),
        meta: {
          title: "发起流程",
          roles: ["admin"]
        },
        hidden: true
      }, */
      {
        path: "appvove",
        name: "TaskAppvove",
        component: () => import("@/views/my-flow/flow-approve.vue"),
        meta: {
          title: "待我处理",
          roles: ["admin"]
        },
      },
      {
        path: "record",
        name: "TaskRecord",
        component: () => import("@/views/my-flow/flow-record.vue"),
        meta: {
          title: "已处理的",
          roles: ["admin"]
        },
      },
      {
        path: "my",
        name: "FlowMy",
        component: () => import("@/views/my-flow/flow-my.vue"),
        meta: {
          title: "我发起的",
          roles: ["admin"]
        },
      },
      {
        path: "cc",
        name: "TaskCc",
        component: () => import("@/views/my-flow/flow-cc.vue"),
        meta: {
          title: "抄送我的",
          roles: ["admin"]
        },
      },
      {
        path: "draft",
        name: "TaskDraft",
        component: () => import("@/views/my-flow/flow-draft.vue"),
        meta: {
          title: "我的草稿",
          roles: ["admin"]
        },
      },
    ],
  },
  {
    path: "/flow",
    name: "flow",
    redirect: "/flow/category",
    component: Layout,
    meta: {
      title: "流程管理",
      icon: "menu-flow-manage",
      roles: ["admin"]
    },
    children: [{
        path: "category",
        name: "FlowCategory",
        component: () => import("@/views/flow/category/index.vue"),
        meta: {
          title: "流程分类",
          roles: ["admin"]
        },
      },
      {
        path: "form",
        name: "FlowForm",
        component: () => import("@/views/flow/form/index.vue"),
        meta: {
          title: "流程表单",
          roles: ["admin"]
        },
        children: [{
          path: "design",
          name: "FormDesign",
          component: () => import("@/views/flow/form/design.vue"),
          meta: {
            title: "设计表单",
            roles: ["admin"]
          },
          hidden: true,
        }]
      },
      {
        path: "model",
        name: "FlowModel",
        component: () => import("@/views/flow/flow-manage/index.vue"),
        meta: {
          title: "流程模型",
          roles: ["admin"]
        },
      },
      {
        path: "instance",
        name: "FlowInstance",
        component: () => import("@/views/flow/instance/index.vue"),
        meta: {
          title: "流程实例",
          roles: ["admin"]
        },
      },
    ],
  },
  {
    path: "/form/design",
    name: "FormDesign",
    component: () => import("@/views/flow/form/design.vue"),
    meta: {
      title: "设计表单",
      roles: ["admin"]
    },
    hidden: true,
  },
  {
    path: "/task/start/apply",
    name: "TaskStartApply",
    component: () => import("@/views/my-flow/flow-start-form.vue"),
    meta: {
      title: "发起流程",
      transition: "fade-transform",
      roles: ["admin"]
    },
    hidden: true
  },
  {
    path: "/flow/edit",
    name: "FlowEdit",
    component: () => import("@/views/flow/flow-manage/flow-edit.vue"),
    meta: {
      title: "流程模型",
      transition: "fade-transform",
      roles: ["admin"]
    },
    hidden: true,
  },
  {
    path: "/flow/detail",
    name: "FlowDetail",
    component: () => import("@/views/my-flow/wx-flow-detail.vue"),
    meta: {
      title: "流程详细",
      transition: "fade-transform",
      roles: ["admin"]
    },
    hidden: true,
  },
];

export const asyncRoutes = [];

// 遍历子路由，全部转换为绝对路径
const fixChildrenRoute = (parent, children) => {
  let prefix = parent.path === "/" ? "" : parent.path;
  (children || []).forEach((child) => {
    let {
      path,
      name
    } = child;
    if (!path.startsWith("/")) child.path = `${prefix}/${path}`;
    if (!name.startsWith("/")) child.name = `${prefix}/${name}`;
    fixChildrenRoute(child, child.children);
  });
};

// 遍历路由，全部转换为绝对路径
let fixRoutes = (routes) => {
  routes.forEach((route) => {
    fixChildrenRoute(route, route.children);
  });
  return routes;
};

const newRouter = () => {
  return createRouter({
    scrollBehavior: () => ({
      top: 0
    }),
    history: createWebHashHistory(),
    routes: fixRoutes(constantRoutes),
  });
};

const router = newRouter();

export function resetRouter() {
  const router0 = newRouter();
  router.matcher = router0.matcher;
}

export default router;